﻿using DataprotoBuf;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using WebApplication_Simple_Server.Models;


//注意事项SingleR 服务端方法不支持方法重载
//如果客户端调用方法明相同，参数个数不同貌似不会被调用
namespace WebApplication_Simple_Server
{
    public class MyHub : Hub<IClientConsloe>
    {

        //被客户端调用
        public async Task CallByClientAsync(string cliStr1, string cliStr2)
        {
            await Task.Delay(10);
            Console.WriteLine($"+++++++++++++客户端调用{MethodBase.GetCurrentMethod()?.Name}+++++++");
            Console.WriteLine($"参数1 {cliStr1}\t参数2 {cliStr2}");
            Console.WriteLine($"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        }

        public async Task<string> CallByClientByReturnValueAsync(string str1)
        {
            await Task.Delay(10);
            Console.WriteLine($"+++++++++++++客户端调用{MethodBase.GetCurrentMethod()?.Name}+++++++");
            Console.WriteLine($"参数1 {str1}");
            Console.WriteLine($"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            return $"{DateTime.Now}{ MethodBase.GetCurrentMethod()?.Name}";
        }

        //调用客户端的的方法,客户端谁调用我，我回调谁
        public async Task CallClientByCallerAsync(string str)
        {
            await Task.Delay(10);
            Console.WriteLine($"+++++++++++++++服务端  ------> 客户端方法++++++++++++++++");
            Console.WriteLine($"clientID：{Context.ConnectionId}");
            await Clients.Caller.ShowMessageAsync(str);
            Console.WriteLine($"+++++++++++++{MethodBase.GetCurrentMethod()?.Name}+++++++");
        }

        //调用所有客户端的方法
        public async Task CallClientByAllAsync(string str)
        {
            await Task.Delay(10);
            Console.WriteLine($"+++++++++++++++服务端  ------> 客户端方法++++++++++++++++");
            Console.WriteLine($"clientID：{Context.ConnectionId}");
            await Clients.All.ShowMessageAsync(str);

            Console.WriteLine($"+++++++++++++{MethodBase.GetCurrentMethod()?.Name}+++++++");
        }


        //向自身以外全部发送
        public async Task CallClientByExceptMySelfAsync(string str)
        {
            await Task.Delay(10);
            Console.WriteLine($"+++++++++++++++服务端  ------> 客户端方法++++++++++++++++");
            Console.WriteLine($"clientID：{Context.ConnectionId}");
            await Clients.Others.ShowMessageAsync(str);
            Console.WriteLine($"+++++++++++++{MethodBase.GetCurrentMethod()?.Name}+++++++");
        }



        public async Task<string> SendMessageAsync2(string identify, IOTModel model)
        {
            await Task.Delay(100);
            Console.WriteLine(MethodBase.GetCurrentMethod()?.Name + "客户端调用方法，" + identify);
            return DateTime.Now.ToString();
        }
        public async Task<string> SendMessageAsync(string identify)
        {
            await Task.Delay(100);
            Console.WriteLine(MethodBase.GetCurrentMethod()?.Name + "客户端调用方法，" + identify);
            return DateTime.Now.ToString();
        }


    }
}
